package com.sanploy.server.util;

import com.google.gson.Gson;
import com.sanploy.server.okhttp.OKhttpUtil;
import com.sanploy.server.websocket.MySocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * Created by 1 on 2017/8/25.
 */
@Component
public class WechatAsyncTask {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    private Gson gson = new Gson();

    @Autowired
    private SockUtil sockUtil;

    @Autowired
    private MySocketHandler mySocketHandler;

    @Async("mySimpleAsync")
    public void monitorAttention(String requestUrl, String name, int flag,ConnectUserInfo connectUserInfo) throws InterruptedException, IOException {

        if (flag > 10) {  //已关注执行
            mySocketHandler.pushSuccessful(name,connectUserInfo.getNickname());
        } else {
            while (flag < 10) {
                String json = OKhttpUtil.sendGet(requestUrl);
                System.out.println(json);
                connectUserInfo = gson.fromJson(json, ConnectUserInfo.class);
                if (connectUserInfo.getSubscribe() == 1) {
                    mySocketHandler.pushSuccessful(name,connectUserInfo.getNickname());
                    return;
                }
                flag++;
                Thread.sleep(1000);
            }
        }
    }

//    @Async("myAsync")
//    public Future<String> doTask2() throws InterruptedException{
//        logger.info("Task2 started.");
//        long start = System.currentTimeMillis();
//        Thread.sleep(3000);
//        long end = System.currentTimeMillis();
//
//        logger.info("Task2 finished, time elapsed: {} ms.", end-start);
//
//        return new AsyncResult<>("Task2 accomplished!");
//    }
}
